package cn.lifeaid.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.lifeaid.db.SysOrganization;
import cn.lifeaid.db.SysOrganizationExample;

@Service
public class SysOrganizationServiceImpl implements SysOrganizationService {

	@Autowired
	SqlSession sqlSession;

	@Override
	public void createOrganization(SysOrganization organization) {
		sqlSession.insert("cn.lifeaid.db.SysOrganizationMapper.insert",
				organization);
	}

	@Override
	public void updateOrganization(SysOrganization organization) {
		sqlSession.update(
				"cn.lifeaid.db.SysOrganizationMapper.updateByPrimaryKey",
				organization);

	}

	@Override
	public void deleteOrganization(Long organizationId) {
		SysOrganization organization = findOne(organizationId);
		sqlSession.delete(
				"cn.lifeaid.db.SysOrganizationMapper.deleteByPrimaryKey",
				organizationId);
		// 同时删除以该组织机构为父级组织机构的组
		SysOrganizationExample example = new SysOrganizationExample();
		SysOrganizationExample.Criteria criteria = example.or();
		criteria.andParentIdsLike(makeSelfAsParentIds(organization) + "%");
		sqlSession.delete(
				"cn.lifeaid.db.SysOrganizationMapper.deleteByExample", example);
	}

	@Override
	public SysOrganization findOne(Long organizationId) {
		return sqlSession.selectOne(
				"cn.lifeaid.db.SysOrganizationMapper.selectByPrimaryKey",
				organizationId);
	}

	@Override
	public List<SysOrganization> findAll() {
		return sqlSession.selectList(
				"cn.lifeaid.db.SysOrganizationMapper.selectByExample",
				new SysOrganizationExample());
	}

	@Override
	public Object findAllWithExclude(SysOrganization excludeOraganization) {
		String paraentIds = makeSelfAsParentIds(excludeOraganization);
		SysOrganizationExample example = new SysOrganizationExample();
		SysOrganizationExample.Criteria criteria = example.or();
		criteria.andIdNotEqualTo(excludeOraganization.getId());
		criteria.andParentIdsNotLike(paraentIds + "%");
		return sqlSession.selectList(
				"cn.lifeaid.db.SysOrganizationMapper.selectByExample", example);
	}

	@Override
	public void move(SysOrganization source, SysOrganization target) {
		SysOrganizationExample example = new SysOrganizationExample();
		SysOrganizationExample.Criteria criteria = example.or();
		criteria.andIdEqualTo(source.getId());
		SysOrganization org = new SysOrganization();
		org.setParentId(target.getParentId());
		org.setParentIds(target.getParentIds());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("record", org);
		map.put("example", example);
		sqlSession.update(
				"cn.lifeaid.db.SysOrganizationMapper.updateByExample", map);
		map.clear();
		map.put("targetAsParentIds", makeSelfAsParentIds(target));
		map.put("sourceAsParentIds", makeSelfAsParentIds(source));
		map.put("likeSourceAsParentIds", makeSelfAsParentIds(source) + "%");
		sqlSession
				.update("cn.lifeaid.db.SysOrganizationMapper.updateSysOrgainzationMove",
						map);

	}

	/**
	 * 将当前组织机构设置为父组织机构
	 * 
	 * @param organization
	 * @return
	 */
	public String makeSelfAsParentIds(SysOrganization organization) {
		return organization.getParentIds() + organization.getId() + "/";
	}
}
